package com.example.myapplication;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

public class ProfileEditActivity extends AppCompatActivity {
    private String username;
    private Button btnBack;
    private TextView tvUsername;
    private TextView tvInterest;
    private ActivityResultLauncher<Intent> interestLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_edit);

        // 接收用户名
        username = getIntent().getStringExtra("username");

        // 初始化控件
        btnBack = findViewById(R.id.btn_back);
        tvUsername = findViewById(R.id.tv_username);
        tvInterest = findViewById(R.id.tv_interest);

        // 显示用户名
        tvUsername.setText("用户名：" + username);

        // 返回按钮点击事件，添加点击反馈
        btnBack.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // 按下时颜色变深
                        btnBack.setBackgroundColor(Color.DKGRAY);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        // 松开时恢复
                        btnBack.setBackgroundColor(Color.TRANSPARENT);
                        break;
                }
                return false;
            }
        });

        btnBack.setOnClickListener(v -> finish());

        // 注册回调：接收从 InterestActivity 返回的数据
        interestLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if (result.getResultCode() == RESULT_OK) {
                            Intent data = result.getData();
                            if (data != null) {
                                String selectedInterest = data.getStringExtra("select_interest");
                                tvInterest.setText("兴趣：" + selectedInterest);
                            }
                        }
                    }
                }
        );

        // 兴趣文本框点击事件
        tvInterest.setOnClickListener(v -> {
            Intent intent = new Intent(ProfileEditActivity.this, InterestActivity.class);
            interestLauncher.launch(intent);
        });
    }
}
